<?php

declare(strict_types=1);

namespace App\Model;


/**
 * @property int $id ID
 * @property int $userid 用户ID
 * @property int $friend_userid 朋友用户ID
 * @property int $last_time 最后时间
 * @property int $add_time 添加时间
 * @property-read null|User $user 
 * @property-read null|Chat $lastChat 
 */
class Friend extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'friend';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'userid', 'friend_userid', 'last_time', 'add_time'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'userid' => 'integer', 'friend_userid' => 'integer', 'last_time' => 'integer', 'add_time' => 'integer'];

    public function user(){
        return $this->hasOne(User::class, 'friend_userid', 'userid')->select(['id', 'username', 'nickname', 'avatar', 'role_id']);
    }

    public function lastChat(){
        return $this->hasOne(Chat::class, ['userid', 'friend_userid'], ['from_userid', 'to_userid']);
    }
}
